<template>
  <div>
    <div>
      <template v-if="['char'].indexOf(field.type) >= 0">
        <field-char :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="[ 'text', ].indexOf(field.type) >= 0">
        <field-text :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="[ 'integer'].indexOf(field.type) >= 0">
        <field-integer :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="[ 'float'].indexOf(field.type) >= 0">
        <field-float :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="[  'datetime'].indexOf(field.type) >= 0">
        <field-date-time :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="[ 'date'].indexOf(field.type) >= 0">
        <field-date :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>

      <template v-else-if="field.type === 'boolean'">
        <field-boolean :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="field.type === 'binary'">
        <field-binary-img :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="['many2one'].indexOf(field.type) >= 0">
        <field-many2one :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="['selection'].indexOf(field.type) >= 0">
        <field-selection :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="['many2many'].indexOf(field.type) >= 0">
        <Many2manyTags v-if="field.widget==='tag'" :model="model" :id="id" :field="field" :title="title" :edit="edit"
                       :saveFast="saveFast"/>
        <Many2manyForm v-else :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>
      <template v-else-if="['one2many'].indexOf(field.type) >= 0">
        <Many2manyTags v-if="field.widget==='tag'" :model="model" :id="id" :field="field" :title="title" :edit="edit"
                       :saveFast="saveFast"/>
        <Many2manyForm v-else :model="model" :id="id" :field="field" :title="title" :edit="edit" :saveFast="saveFast"/>
      </template>

      <template v-else-if="['html'].indexOf(field.type) >= 0 ">
        <field-html :model="model" :id="id" :field="field" :edit="edit" :title="title" :saveFast="saveFast"/>
      </template>
      <template v-else-if="'reference'===field.type">
        <field-reference :model="model" :id="id" :field="field" :edit="edit" :title="title" :saveFast="saveFast"/>
      </template>

      <template v-else-if="!field.invisible && field.type === 'button'">
        <div :have_button="button=true"/>
      </template>
    </div>
  </div>
</template>
<script>
  import Many2manyTags from './fields/Many2manyTags'
  import FieldInteger from './fields/FieldInteger'
  import FieldChar from './fields/FieldChar'
  import FieldBinaryImg from './fields/FieldBinaryImg'
  import FieldBoolean from './fields/FieldBoolean'
  import FieldMany2one from './fields/FieldMany2one'
  import FieldSelection from './fields/FieldSelection'
  import FieldText from './fields/FieldText'
  import FieldDateTime from './fields/FieldDateTime'
  import FieldDate from './fields/FieldDate'
  import FieldFloat from './fields/FieldFloat'
  import FieldHtml from './fields/FieldHtml'
  import FieldReference from './fields/FieldReference'
  import Many2manyForm from "./fields/Many2manyForm";

  export default {
  name: 'Field',
  components: {
    Many2manyForm,
    FieldSelection,
    FieldInteger,
    FieldChar,
    FieldBinaryImg,
    FieldBoolean,
    Many2manyTags,
    FieldMany2one,
    FieldText,
    FieldDateTime,
    FieldDate,
    FieldFloat,
    FieldHtml,
    FieldReference
  },
  props: {
    model: {type: String, default: ''},
    id: {type: Number, default: 0},
    field: {
      type: Object,
      default() {
        return {}
      }
    },
    value: {
      type: Object,
      default() {
        return {}
      }
    },
    context: {
      type: Object,
      default() {
        return {}
      }
    },
    edit: { type: Boolean, default: false },
    saveFast: {type: Boolean, default: true},
    title: { type: Boolean, default: false }
  },
  data: function () {
    return {
      button: false,
      newvalue: this.value
    }
  },
    provide() {
      return {
        value: this.value,
        getValue: () => this.value
    }
  },
    watch: {
      // value () {
      //   this.$emit('update:value', this.newvalue)
      // },
      // '$refs.childtype.value' () {
      //   this.newvalue = this.$refs.childtype.newvalue
      // }
  }

  // watch: { field: function (value) {
  //   this.$emit('update:field', this.field)
  // } }
}
</script>

<style>
</style>
